package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.ChartsDetailActivity;
import com.lly.songhigh.activity.WebViewActivity;
import com.lly.songhigh.adapter.ChartsListViewAdapter;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.RankingList;
import com.lly.songhigh.view.NoScrollListView;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class ChartsFragment extends BaseFragment implements AdapterView.OnItemClickListener {


    private TextView chartsOfficialTitle;
    private NoScrollListView chartsOfficialListView;
    private TextView chartsSpecialTitle;
    private NoScrollListView chartsSpecialListView;
    private TextView chartsGlobalTitle;
    private NoScrollListView chartsGlobalListView;
    private View chartsDividerFirst;

    private View chartsDividerSecond;

    private ChartsListViewAdapter adapter;
    private VolleySingleton volleySingleton;
    private List<RankingList> rankingLists;
    private List<String> names;
    private List<String> sub_titles;
    private List<RankingList.RefsEntity> refsEntities;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_charts, null);
        initView(view);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.KEYCODE_BACK) {
                }
                return false;
            }
        });
    }

    private void initView(View view) {
        chartsOfficialTitle = (TextView) view.findViewById(R.id.charts_official_title);
        chartsOfficialListView = (NoScrollListView) view.findViewById(R.id.charts_official_list_view);
        chartsSpecialTitle = (TextView) view.findViewById(R.id.charts_special_title);
        chartsSpecialListView = (NoScrollListView) view.findViewById(R.id.charts_special_list_view);
        chartsGlobalTitle = (TextView) view.findViewById(R.id.charts_global_title);
        chartsGlobalListView = (NoScrollListView) view.findViewById(R.id.charts_global_list_view);
        chartsDividerFirst = view.findViewById(R.id.charts_divider_first);

        chartsDividerSecond = view.findViewById(R.id.charts_divider_second);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance();
        initData();
        chartsOfficialListView.setOnItemClickListener(this);
        chartsSpecialListView.setOnItemClickListener(this);
        chartsGlobalListView.setOnItemClickListener(this);
    }

    private void initData() {
        names = new ArrayList<>();
        sub_titles = new ArrayList<>();
        final StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.CHARTS_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                chartsDividerFirst.setVisibility(View.VISIBLE);
                chartsDividerSecond.setVisibility(View.VISIBLE);
                Gson gson = new Gson();
                rankingLists = gson.fromJson(response, new TypeToken<List<RankingList>>() {
                }.getType());
                adapter = new ChartsListViewAdapter(getActivity(), rankingLists, StringResouce.GOVERNMENT);
                chartsOfficialListView.setAdapter(adapter);
                adapter = new ChartsListViewAdapter(getActivity(), rankingLists, StringResouce.SPECIAL);
                chartsSpecialListView.setAdapter(adapter);
                adapter = new ChartsListViewAdapter(getActivity(), rankingLists, StringResouce.WORLD);
                chartsGlobalListView.setAdapter(adapter);
                for (RankingList rankingList : rankingLists) {
                    names.add(rankingList.getName());
                    refsEntities = rankingList.getRefs();
                }

                for (RankingList.RefsEntity refsEntity : refsEntities) {
                    sub_titles.add(refsEntity.getSub_title());
                }
                chartsOfficialTitle.setText(names.get(0) + "-" + sub_titles.get(0));
                chartsSpecialTitle.setText(names.get(1) + "-" + sub_titles.get(1));
                chartsGlobalTitle.setText(names.get(2) + "-" + sub_titles.get(2));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RankingList.RefsEntity refsEntity = (RankingList.RefsEntity) parent.getItemAtPosition(position);
        if (refsEntity.getRanklist_id() != -1 ) {
            int ranklist_id = refsEntity.getRanklist_id();
            Intent intent = new Intent(getActivity(), ChartsDetailActivity.class);
            intent.putExtra("ranklist_id", ranklist_id);
            startActivity(intent);
        } else {
            String link = refsEntity.getLink();
            Intent intent = new Intent(getActivity(), WebViewActivity.class);
            intent.putExtra("dese", refsEntity.getDesc());
            intent.putExtra("link", link);
            startActivity(intent);
        }
    }
}
